<template>
  <a-modal
    title="编辑客户联系人"
    width="80%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
        <a-form-item
          label="客户资料名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-select
            placeholder="请选择客户资料名称"
            v-decorator="['cusInforId',{rules: [{ required: true, message: '请选择客户资料名称！'}]}]">
            <a-select-option
              v-for="(item,index) in cusInforList"
              :key="index"
              :value="item.id">{{ item.cusInforName }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="联系人"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入联系人" v-decorator="['cusPerson', {rules: [{required: true, message: '请输入联系人！'}]}]" />
        </a-form-item>
        <a-form-item
          label="联系电话"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input
            placeholder="请输入联系电话"
            allowClear
            :maxLength="11"
            v-decorator="['cusPhone', {rules: [{required: true, validator: validatorPhone}]}]" />
        </a-form-item>
        <a-form-item
          label="主联系人"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-radio-group
            placeholder="是否为主联系人"
            v-decorator="['maiorCusPerson']">
            <a-radio v-for="(item,index) in maiorCusPersonData" :key="index" :value="item.code">{{
              item.name
            }}
            </a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item
          label="备注信息"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-textarea placeholder="请输入备注信息" v-decorator="['remark']" :auto-size="{ minRows: 3, maxRows: 6 }"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { cusPersonEdit } from '@/api/modular/main/cusperson/cusPersonManage'
  import { cusInforList } from '@/api/modular/main/cusinfor/cusInforManage'
  import XEUtils from 'xe-utils'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        cusInforList: [],
        maiorCusPersonData: [
          {
            name: '是',
            code: 1
          },
          {
            name: '否',
            code: -1
          }
        ],
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      // 初始化方法
      edit (record) {
        this.visible = true
        cusInforList().then(res => {
          this.cusInforList = res.data
        })
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              id: record.id,
              cusInforId: record.cusInforId,
              cusPerson: record.cusPerson,
              cusPhone: record.cusPhone,
              maiorCusPerson: parseInt(record.maiorCusPerson),
              remark: record.remark
            }
          )
        }, 100)
      },
      /**
       * 手机号码校验
       */
      validatorPhone (rule, value) {
        const phoneReg = /^1[3|4|5|7|8][0-9]{9}$/
        if (value === undefined) {
          return Promise.reject(new Error('请输入电话号码！'))
        } else {
          if (!phoneReg.test(value)) {
            return Promise.reject(new Error('请输入正确格式的电话号码！'))
          }
          return Promise.resolve()
        }
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            values.cusPerson = XEUtils.trim(values.cusPerson)
            values.cusPhone = XEUtils.trim(values.cusPhone)
            values.maiorCusPerson = XEUtils.trim(values.maiorCusPerson)
            for (const key in values) {
              if (typeof (values[key]) === 'object' && values[key] != null) {
                values[key] = JSON.stringify(values[key])
              }
            }
            cusPersonEdit(values).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error(res.message)//  + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
